package com.zjuee.web.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zjuee.web.auth.mapper.UserRoleMapper;
import com.zjuee.base.exception.ServiceException;
import com.zjuee.base.model.common.auth.UserRole;
import com.zjuee.base.utils.CommonUtil;
import com.zjuee.service.auth.service.UserRoleService;
import com.zjuee.service.auth.vo.MenuVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

/**
 * @author wu liang
 * @since 1.0
 */
@Slf4j
@Service
public class UserRoleServiceImpl implements UserRoleService {

    @Resource
    private UserRoleMapper userRoleMapper;

//    @Resource
//    private LoginDto loginDto;

    @Override
    public boolean deleteByRoleId(Long roleId) {
        return userRoleMapper.delete(fuzzyQuery("role_id", roleId)) > 0;
    }

    @Override
    public boolean deleteByUserId(Long userId) {
        return userRoleMapper.delete(fuzzyQuery("user_id", userId)) > 0;
    }

    @Override
    public Integer findAllUserByRoleId(Long id) {
        return userRoleMapper.selectCount(fuzzyQuery("role_id", id));
    }

    @Override
    public List<MenuVo> findAllMenus(Long userId) {
//        AuthUser user = SecurityUtil.getOnlineUser();
//        if (user == null || user.getUserId() == null) {
//            return new ArrayList<>();
//        }

        List<MenuVo> menus = null;
//        if (Objects.equals(UserType.superadmin.toString(), user.getUserType())) {
//            menus = menuMapper.findAll();
//        }
//        //涉及到一个问题，如果用户只分配了子级资源，如何展示
//        //这里我们定义，如果只分配了子级资源，那么也同步级联展示父级资源
//        //尽管有可能会有重复结果集，这里还需使用list接收，为了保证排序（递归重构结果集后将没有重复）
//        else {
            //根据用户id查询菜单列表
            menus = userRoleMapper.findAllMenuByUserId(userId);
//        }
        return CommonUtil.buildTree(menus, MenuVo::getLevelCode, MenuVo::getChildren);
    }

    @Override
    public UserRole getUserRole(Long userId) {
        LambdaQueryWrapper<UserRole> queryWrapper = Wrappers.<UserRole>query().lambda();
        queryWrapper.eq(UserRole::getUserId,userId);
        return userRoleMapper.selectOne(queryWrapper);
    }

    @Override
    public Long add(UserRole userRole) {
        if (userRoleMapper.insert(userRole) > 0) {
            return userRole.getId();
        }
        throw new ServiceException("The user role add failed");
    }

    //根据用户id更新信息
    @Override
    public boolean update(UserRole type) {
        return userRoleMapper.updateById(type) > 0;
    }


    //获取查询条件
    public QueryWrapper<UserRole> fuzzyQuery(String columnName, Long id) {
        QueryWrapper<UserRole> queryWrapper = Wrappers.query();
        Optional.ofNullable(id).ifPresent(x -> {
            queryWrapper.eq(columnName, x);
        });
        return queryWrapper;
    }
}
